import QtQuick 1.1

DialogLiquid {
    id: dlgSubscribes

    height: parent.height*0.5

    property string selectedBareJidSubscribe: ""

    function addBareJid( bareJid )
    {
        if( bareJid != "" ) {
            modelListBareJids.append( { bareJid : bareJid } )
        }
    }

    ListModel {
        id: modelListBareJids
        //ListElement { bareJid: "bareJid1@server1.com" } //as example
    }

    Component {
        id: componentWrapper
        Rectangle {
            id: wrapper
            width: listViewJids.width
            height: 70
            border.color: "gray"
            Text {
                id: txtItem
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 20
                text: bareJid
                font.pixelSize: 28
            }
            MouseArea {
                id: mouseAreaItem;
                anchors.fill: parent
                onClicked: {
                    wrapper.ListView.view.currentIndex = index
                    selectedBareJidSubscribe = bareJid
                }
            }

            Component.onCompleted:  {
                if( index == 0 ) {
                    selectedBareJidSubscribe = bareJid
                    //console.log("*** selected: " + selectedBareJidSubscribe)
                }
            }

            states: State {
                name: "Current"
                when: wrapper.ListView.isCurrentItem
                PropertyChanges { target: wrapper; color: "lightblue" }
            }
        }
    }

    Rectangle {
        id: headerDlg
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        height: 70
        gradient: Gradient {
            GradientStop { position: 0; color: "#919191" }
            GradientStop { position: 1; color: "#666666" }
        }
        Text {
            anchors.centerIn: parent
            font.pixelSize: 22
            color: "white"
            text: qsTr("Authorization requests")
        }
    }


    property int __marg1 : 3
    ListView {
        id: listViewJids
        anchors.top: headerDlg.bottom
        anchors.topMargin: __marg1
        anchors.left: parent.left
        anchors.leftMargin: __marg1
        anchors.bottom: rowOfButtons.top
        anchors.bottomMargin: __marg1
        width: parent.width - __marg1*2
        model: modelListBareJids
        delegate: componentWrapper
        clip: true
    }

    Item {
        id:rowOfButtons
        width: parent.width - 2*__marg1
        height: 80
        anchors.left: parent.left
        anchors.leftMargin: __marg1
        anchors.bottom: parent.bottom
        anchors.bottomMargin: __marg1
        Button {
            id: buttonAccept
            text: qsTr("Accept")
            height: 70
            width: parent.width/2 - 10
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.leftMargin: 4
            onClicked: {
                var res = xmppClient.acceptSubscribtion( selectedBareJidSubscribe )
                if( res ) {
                    for( var j=0; j<modelListBareJids.count; j++ ) {
                        var jid = modelListBareJids.get(j).bareJid
                        if( jid == selectedBareJidSubscribe ) {
                            modelListBareJids.remove(j);
                            break;
                        }
                    }
                } //if(res)
            }
        }
        Button {
            id: buttonReject
            text: qsTr("Reject")
            height: 70
            width: parent.width/2 - 10
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: parent.right
            anchors.rightMargin: 4
            onClicked: {
                var res = xmppClient.rejectSubscribtion( selectedBareJidSubscribe )
                if( res ) {
                    for( var j=0; j<modelListBareJids.count; j++ ) {
                        var jid = modelListBareJids.get(j).bareJid
                        if( jid == selectedBareJidSubscribe ) {
                            modelListBareJids.remove(j);
                            break;
                        }
                    }
                } //if(res)
            }
        }
    }

}
